//index.js
//获取应用实例
const app = getApp();
var router = require("../../router.js");
Page({
  data: {
    login: 0,
    content: 1, //内容
    tabBar: 1,//底部
    logo: false,     //登录框
    safe: [ //保险套餐数据          
    ],
    active_list: [ //户外活动数据 收费                       
    ],
    free_active_list: [ //户外活动数据 免费            
    ],
    imgUrls: [ //banner图片数据          
    ],
    juvenile: [
    ],
    indicatorDots: true, //是否添加轮播小圆点
    autoplay: true, //是否自动播放
    interval: 5000, //轮播间隔时间
    duration: 1000, //动画过渡时间
    user: 3,
    userPid: 0,
    // 底部菜单数据
    now_footer_index: 0,
    footer_nav: [
      {
        name: '首页',
        icon: '/image/footer/home2.png',
        active_icon: '/image/footer/home.png'
      },
      {
        name: '团建课程',
        icon: '/image/footer/course2.png',
        active_icon: '/image/footer/course.png'
      },
      {
        name: '个人活动',
        icon: '/image/footer/active2.png',
        active_icon: '/image/footer/active.png'
      },
      {
        name: '我的',
        icon: '/image/footer/person2.png',
        active_icon: '/image/footer/person.png'
      }
    ],
  },
  go_page_buy(e) {  //跳转购买保险页      
    let id = e.currentTarget.dataset.id;
    app.navTo('../buy/buy?id=' + id)
  },
  go_page(e) {
    var path = e.currentTarget.dataset.path;
    wx.navigateTo({
      url: path,
    })
  },
  to_detail(e) {  //跳转活动详情页   
    var info_type = e.currentTarget.dataset.type;
    var index = e.currentTarget.dataset.index;
    var list = '';
    if (info_type == 1) {
      list = this.data.active_list;
    } else if (info_type == 2) {
      list = this.data.free_active_list;
    } else {
      list = this.data.juvenile;
    }
    app.navTo('../active_detail/active_detail?id=' + list[index].id)
  },
  go_buy_safe(event) {
    var index = event.currentTarget.dataset.id;
    app.navTo('../buy/buy?id=' + this.data.safe[index]['id'])
  },
  onLoad: function (options) {
    var scene = decodeURIComponent(options.scene)
    if (!isNaN(scene)) {
      this.setData({
        userPid: scene
      })
    }
    wx.hideTabBar({});
  },
  onShow: function () {
    this.isLogin();
    var that = this;
    app.request({
      url: router.index.audit,
      method: "GET",
      success: function (data) {
        console.log(data)
        that.setData({
          login: data.login,
          content: data.content,
          tabBar: data.tabbar,
        })
      }
    });
    this.getInfo();
    wx.showShareMenu({
      withShareTicket: true
    })
  },
  change_footer_nav(e){
    let index = e.currentTarget.dataset.index;
    this.setData({
      now_footer_index:index
    })
  },
  onShareAppMessage: function (res) {
    var path = '/pages/index_new/index_new';
    if (wx.getStorageSync('userInfo')) {
      var id = wx.getStorageSync("userInfo")['id'];
      path += '?scene=' + id;
    }
    return {
      title: "诺创云拓",
      path: path,
    }
  },
  sendRequest: function (e) {
    var that = this;
    wx.showLoading({
      title: '请稍后',
      success: function () {
        wx.login({
          success: function (result) {
            console.log('result',result)
            wx.request({
              url: router.user.login,
              method: "POST",
              data: {
                code: result.code,
                encryptedData: e.detail.encryptedData,
                iv: e.detail.iv,
                userPid: that.data.userPid
              },
              success: function (res) {
                if (res.data.status === true) {
                  wx.setStorageSync("userInfo", res.data.user)
                  wx.setStorageSync("grade", res.data.grade);
                  that.setData({
                    logo: false
                  })
                  wx.hideLoading();
                  wx.showToast({
                    title: '恭喜你登陆成功',
                    duration: 2000,
                    success: function () {
                      wx.showTabBar({});
                      that.getInfo();
                    }
                  })
                } else {
                  wx.hideLoading();
                  wx.showToast({
                    title: '登录失败,请重新点击授权',
                    content: '请重新点击授权',
                    icon: 'none'
                  })
                }
              },
              fail: function () {
                wx.hideLoading();
                wx.showToast({
                  title: '网络错误',
                  content: '请重新点击授权',
                  icon: 'none'
                })
              }
            })
          }
        });
      }
    });
  },

  isLogin: function () {
    var userInfo = wx.getStorageSync("userInfo");
    if (userInfo) {
      wx.showTabBar({})
      this.setData({
        logo: false
      })
    } else {
      this.setData({
        logo: true
      })
    }
  },
  getInfo: function () {
    if (wx.getStorageSync('userInfo')) {
      var that = this;
      app.request({
        url: router.index.index,
        method: "POST",
        data: {
          userId: wx.getStorageSync('userInfo').id,
        },
        success: function (data) {
          if (data.status == true) {
            that.setData({
              user: data.user.type,
              safe: data.safe,
              active_list: data.expand,
              free_active_list: data.free_expand,
              juvenile: data.juvenile,
              imgUrls: data.banner,
            });
          } else {
            wx.showToast({
              title: '获取信息失败',
              icon: 'none',
              mask: true,
              duration: 2000
            });
          }
        }
      })
    }
  }
})











